<<<<<<< HEAD
public class Test {
    public List<String> topKFrequent(String[] words, int k) {
        //通过HashMap计算单词出现的次数
        HashMap<String,Integer> map = new HashMap<>();
        for (String str : words) {
            map.put(str,map.getOrDefault(str,0)+1);
        }
        //建小根堆，解决Top-K问题，注意单词顺序，此处a比b要大
        PriorityQueue<String> minHeap = new PriorityQueue<>(k, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(map.get(o1) == map.get(o2)){
                    return o2.compareTo(o1);
                }
                return map.get(o1) - map.get(o2);
            }
        });
        //放元素
        for (String str : map.keySet()) {
            if(minHeap.size() < k){
                minHeap.offer(str);
            }else{
                int value = map.get(minHeap.peek());
                int valueout = map.get(str);
                if(valueout > value || (valueout == value && str.compareTo(minHeap.peek()) < 0)){
                    minHeap.poll();
                    minHeap.offer(str);
                }
            }
        }
        //搞到顺序表中
        ArrayList<String> list = new ArrayList<>();
        while(!minHeap.isEmpty()){
            list.add(minHeap.poll());
        }
        //顺序表逆置
        Collections.reverse(list);
        return list;
    }
=======
public class Test {
    public List<String> topKFrequent(String[] words, int k) {
        //通过HashMap计算单词出现的次数
        HashMap<String,Integer> map = new HashMap<>();
        for (String str : words) {
            map.put(str,map.getOrDefault(str,0)+1);
        }
        //建小根堆，解决Top-K问题，注意单词顺序，此处a比b要大
        PriorityQueue<String> minHeap = new PriorityQueue<>(k, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                if(map.get(o1) == map.get(o2)){
                    return o2.compareTo(o1);
                }
                return map.get(o1) - map.get(o2);
            }
        });
        //放元素
        for (String str : map.keySet()) {
            if(minHeap.size() < k){
                minHeap.offer(str);
            }else{
                int value = map.get(minHeap.peek());
                int valueout = map.get(str);
                if(valueout > value || (valueout == value && str.compareTo(minHeap.peek()) < 0)){
                    minHeap.poll();
                    minHeap.offer(str);
                }
            }
        }
        //搞到顺序表中
        ArrayList<String> list = new ArrayList<>();
        while(!minHeap.isEmpty()){
            list.add(minHeap.poll());
        }
        //顺序表逆置
        Collections.reverse(list);
        return list;
    }
>>>>>>> 83a8b80b6ef099ad7c56ca960b378bd45d44769e
}